package dto

import (
	"gorm.io/gorm"
	"project2/g"
	"project2/model"
	"project2/utils"
	"time"
)

type AppFunDto struct {
	Id           uint      `json:"id"`
	AppId        uint      `json:"appId"`
	Name         string    `json:"name"`
	FunType      uint      `json:"funType"`
	FunScope     uint      `json:"funScope"`
	OwnerType    uint      `json:"ownerType"`
	MobileState  uint      `json:"mobileState"`
	MobileUrl    string    `json:"mobileUrl"`
	FunUrl       string    `json:"funUrl"`
	ParentId     uint      `json:"parentId"`
	ParentIdPath string    `json:"parentIdPath"`
	Deep         uint      `json:"deep"`
	Img          string    `json:"img"`
	OrderNum     uint      `json:"orderNum"`
	State        uint      `json:"state"`
	DelState     uint      `json:"delState"`
	AddUser      string    `json:"addUser"`
	AddUserId    uint      `json:"addUserId"`
	AddTime      time.Time `json:"addTime"`
	EditUser     string    `json:"editUser"`
	EditUserId   uint      `json:"editUserId"`
	LastEditTime time.Time `json:"lastEditTime"`
	ServerNumber uint      `json:"serverNumber"`
	DelTime      time.Time `json:"delTime"`
}

func BuildAppFunDto(_body *map[string]interface{}, dto *AppFunDto) {
	body := *_body
	if body == nil {
		return
	}

	if body["id"] != nil && utils.ParserInt8(body["id"]) > 0 {
		dto.Id = uint(utils.ParserInt8(body["id"]))
	}

	if body["appId"] != nil && utils.ParserInt8(body["appId"]) > 0 {
		dto.AppId = uint(utils.ParserInt8(body["appId"]))
	}

	if body["name"] != nil && body["name"].(string) != "" {
		dto.Name = body["name"].(string)
	}

	if body["funType"] != nil && utils.ParserInt8(body["funType"]) > 0 {
		dto.FunType = uint(utils.ParserInt8(body["funType"]))
	}

	if body["funScope"] != nil && utils.ParserInt8(body["funScope"]) > 0 {
		dto.FunScope = uint(utils.ParserInt8(body["funScope"]))
	}

	if body["ownerType"] != nil && utils.ParserInt8(body["ownerType"]) > 0 {
		dto.OwnerType = uint(utils.ParserInt8(body["ownerType"]))
	}

	if body["mobileState"] != nil && utils.ParserInt8(body["mobileState"]) > 0 {
		dto.MobileState = uint(utils.ParserInt8(body["mobileState"]))
	}

	if body["mobileUrl"] != nil && body["mobileUrl"].(string) != "" {
		dto.MobileUrl = body["mobileUrl"].(string)
	}

	if body["funUrl"] != nil && body["funUrl"].(string) != "" {
		dto.FunUrl = body["funUrl"].(string)
	}

	if body["parentId"] != nil && utils.ParserInt8(body["parentId"]) > 0 {
		dto.ParentId = uint(utils.ParserInt8(body["parentId"]))
	}

	if body["parentIdPath"] != nil && body["parentIdPath"].(string) != "" {
		dto.ParentIdPath = body["parentIdPath"].(string)
	}

	if body["deep"] != nil && utils.ParserInt8(body["deep"]) > 0 {
		dto.Deep = uint(utils.ParserInt8(body["deep"]))
	}

	if body["img"] != nil && body["img"].(string) != "" {
		dto.Img = body["img"].(string)
	}

	if body["orderNum"] != nil && utils.ParserInt8(body["orderNum"]) > 0 {
		dto.OrderNum = uint(utils.ParserInt8(body["orderNum"]))
	}

	if body["state"] != nil && utils.ParserInt8(body["state"]) > 0 {
		dto.State = uint(utils.ParserInt8(body["state"]))
	}

	if body["delState"] != nil && utils.ParserInt8(body["delState"]) > 0 {
		dto.DelState = uint(utils.ParserInt8(body["delState"]))
	}

	if body["addUser"] != nil && body["addUser"].(string) != "" {
		dto.AddUser = body["addUser"].(string)
	}

	if body["addUserId"] != nil && utils.ParserInt8(body["addUserId"]) > 0 {
		dto.AddUserId = uint(utils.ParserInt8(body["addUserId"]))
	}

	if body["addTime"] != nil && !utils.Str2date(body["addTime"].(string)).IsZero() {
		dto.AddTime = utils.Str2date(body["addTime"].(string))
	}

	if body["editUser"] != nil && body["editUser"].(string) != "" {
		dto.EditUser = body["editUser"].(string)
	}

	if body["editUserId"] != nil && utils.ParserInt8(body["editUserId"]) > 0 {
		dto.EditUserId = uint(utils.ParserInt8(body["editUserId"]))
	}

	if body["lastEditTime"] != nil && !utils.Str2date(body["lastEditTime"].(string)).IsZero() {
		dto.LastEditTime = utils.Str2date(body["lastEditTime"].(string))
	}

	if body["serverNumber"] != nil && utils.ParserInt8(body["serverNumber"]) > 0 {
		dto.ServerNumber = uint(utils.ParserInt8(body["serverNumber"]))
	}

	if body["delTime"] != nil && !utils.Str2date(body["delTime"].(string)).IsZero() {
		dto.DelTime = utils.Str2date(body["delTime"].(string))
	}
}
func (self *AppFunDto) BuildQuery(query **gorm.DB) {
	(*query) = g.Db.Model(&model.SysAccount{})
	(*query).Where("del_time is not null")

	if self.Id > 0 {
		(*query).Where("id =?", self.Id)
	}

	if self.AppId > 0 {
		(*query).Where("app_id =?", self.AppId)
	}

	if self.Name != "" {
		(*query).Where("name =?", self.Name)
	}

	if self.FunType > 0 {
		(*query).Where("fun_type =?", self.FunType)
	}

	if self.FunScope > 0 {
		(*query).Where("fun_scope =?", self.FunScope)
	}

	if self.OwnerType > 0 {
		(*query).Where("owner_type =?", self.OwnerType)
	}

	if self.MobileState > 0 {
		(*query).Where("mobile_state =?", self.MobileState)
	}

	if self.MobileUrl != "" {
		(*query).Where("mobile_url =?", self.MobileUrl)
	}

	if self.FunUrl != "" {
		(*query).Where("fun_url =?", self.FunUrl)
	}

	if self.ParentId > 0 {
		(*query).Where("parent_id =?", self.ParentId)
	}

	if self.ParentIdPath != "" {
		(*query).Where("parent_id_path =?", self.ParentIdPath)
	}

	if self.Deep > 0 {
		(*query).Where("deep =?", self.Deep)
	}

	if self.Img != "" {
		(*query).Where("img =?", self.Img)
	}

	if self.OrderNum > 0 {
		(*query).Where("order_num =?", self.OrderNum)
	}

	if self.State > 0 {
		(*query).Where("state =?", self.State)
	}

	if self.DelState > 0 {
		(*query).Where("del_state =?", self.DelState)
	}

	if self.AddUser != "" {
		(*query).Where("add_user =?", self.AddUser)
	}

	if self.AddUserId > 0 {
		(*query).Where("add_user_id =?", self.AddUserId)
	}

	if !self.AddTime.IsZero() {
		(*query).Where("add_time =?", self.AddTime)
	}

	if self.EditUser != "" {
		(*query).Where("edit_user =?", self.EditUser)
	}

	if self.EditUserId > 0 {
		(*query).Where("edit_user_id =?", self.EditUserId)
	}

	if !self.LastEditTime.IsZero() {
		(*query).Where("last_edit_time =?", self.LastEditTime)
	}

	if self.ServerNumber > 0 {
		(*query).Where("server_number =?", self.ServerNumber)
	}

	if !self.DelTime.IsZero() {
		(*query).Where("del_time =?", self.DelTime)
	}

}
func (self *AppFunDto) BuildEntityForAdd(entity *model.AppFun) {

	if self.Id > 0 {
		entity.Id = self.Id
	} else {
		entity.Id = 0
	}

	if self.AppId > 0 {
		entity.AppId = self.AppId
	} else {
		entity.AppId = 0
	}

	if self.Name != "" {
		entity.Name = self.Name
	} else {
		entity.Name = ""
	}

	if self.FunType > 0 {
		entity.FunType = self.FunType
	} else {
		entity.FunType = 0
	}

	if self.FunScope > 0 {
		entity.FunScope = self.FunScope
	} else {
		entity.FunScope = 0
	}

	if self.OwnerType > 0 {
		entity.OwnerType = self.OwnerType
	} else {
		entity.OwnerType = 0
	}

	if self.MobileState > 0 {
		entity.MobileState = self.MobileState
	} else {
		entity.MobileState = 0
	}

	if self.MobileUrl != "" {
		entity.MobileUrl = self.MobileUrl
	} else {
		entity.MobileUrl = ""
	}

	if self.FunUrl != "" {
		entity.FunUrl = self.FunUrl
	} else {
		entity.FunUrl = ""
	}

	if self.ParentId > 0 {
		entity.ParentId = self.ParentId
	} else {
		entity.ParentId = 0
	}

	if self.ParentIdPath != "" {
		entity.ParentIdPath = self.ParentIdPath
	} else {
		entity.ParentIdPath = ""
	}

	if self.Deep > 0 {
		entity.Deep = self.Deep
	} else {
		entity.Deep = 0
	}

	if self.Img != "" {
		entity.Img = self.Img
	} else {
		entity.Img = ""
	}

	if self.OrderNum > 0 {
		entity.OrderNum = self.OrderNum
	} else {
		entity.OrderNum = 0
	}

	if self.State > 0 {
		entity.State = self.State
	} else {
		entity.State = 0
	}

	if self.DelState > 0 {
		entity.DelState = self.DelState
	} else {
		entity.DelState = 0
	}

	if self.AddUser != "" {
		entity.AddUser = self.AddUser
	} else {
		entity.AddUser = ""
	}

	if self.AddUserId > 0 {
		entity.AddUserId = self.AddUserId
	} else {
		entity.AddUserId = 0
	}

	if !self.AddTime.IsZero() {
		entity.AddTime = self.AddTime
	} else {
		entity.AddTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.EditUser != "" {
		entity.EditUser = self.EditUser
	} else {
		entity.EditUser = ""
	}

	if self.EditUserId > 0 {
		entity.EditUserId = self.EditUserId
	} else {
		entity.EditUserId = 0
	}

	if !self.LastEditTime.IsZero() {
		entity.LastEditTime = self.LastEditTime
	} else {
		entity.LastEditTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.ServerNumber > 0 {
		entity.ServerNumber = self.ServerNumber
	} else {
		entity.ServerNumber = 0
	}

}
func (self *AppFunDto) BuildEntityForUpdate(entity *model.AppFun) {
	entity.Id = self.Id

	if self.Id > 0 {
		entity.Id = self.Id
	}

	if self.AppId > 0 {
		entity.AppId = self.AppId
	}

	if self.Name != "" {
		entity.Name = self.Name
	}

	if self.FunType > 0 {
		entity.FunType = self.FunType
	}

	if self.FunScope > 0 {
		entity.FunScope = self.FunScope
	}

	if self.OwnerType > 0 {
		entity.OwnerType = self.OwnerType
	}

	if self.MobileState > 0 {
		entity.MobileState = self.MobileState
	}

	if self.MobileUrl != "" {
		entity.MobileUrl = self.MobileUrl
	}

	if self.FunUrl != "" {
		entity.FunUrl = self.FunUrl
	}

	if self.ParentId > 0 {
		entity.ParentId = self.ParentId
	}

	if self.ParentIdPath != "" {
		entity.ParentIdPath = self.ParentIdPath
	}

	if self.Deep > 0 {
		entity.Deep = self.Deep
	}

	if self.Img != "" {
		entity.Img = self.Img
	}

	if self.OrderNum > 0 {
		entity.OrderNum = self.OrderNum
	}

	if self.State > 0 {
		entity.State = self.State
	}

	if self.DelState > 0 {
		entity.DelState = self.DelState
	}

	if self.AddUser != "" {
		entity.AddUser = self.AddUser
	}

	if self.AddUserId > 0 {
		entity.AddUserId = self.AddUserId
	}

	if !self.AddTime.IsZero() {
		entity.AddTime = self.AddTime
	} else {
		entity.AddTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.EditUser != "" {
		entity.EditUser = self.EditUser
	}

	if self.EditUserId > 0 {
		entity.EditUserId = self.EditUserId
	}

	if !self.LastEditTime.IsZero() {
		entity.LastEditTime = self.LastEditTime
	} else {
		entity.LastEditTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.ServerNumber > 0 {
		entity.ServerNumber = self.ServerNumber
	}

}
